<?php

namespace app\controllers;


use yii\db\Query;
use yii\web\UploadedFile;

class ApiUserController extends ApiBaseController{

    private $user_id;
    public function init(){
        parent::init();
        $this->user_id = $this->GetUser()['user_id'];
    }
    
    
    
     //个人中心简介
     public function actionDesc(){
     $where['user_id']=$this->user_id;	
     $userinfo=(new Query())->select('birthday')->from('yii_user')->where($where)->one();	
     $age=date('Y',time())-date('Y',strtotime($userinfo['birthday'])).'岁';
     $education=(new Query())->select('id,education,start_time,end_time')->from('yii_user_education')->where($where)->orderBy('education desc')->one();
     $year=date('Y',strtotime($education['end_time']))-date('Y',strtotime($education['start_time'])).'年';
     $education=$this->getEducation($education['education']);
       return json_encode(['age'=>$age,'year'=>$year,'education'=>$education]);
     }
     
     
     public function getEducation($id){
     	$where['id']=$id;
        $list=(new Query())->from('yii_education')->where($where)->one();
        return $list['name'];
     }
     
     

    //简历信息
    public function actionIndex(){
      $where['user_id']=$this->user_id;
      $list=(new Query())->select('content')->from('yii_user_evaluate')->where($where)->one();
      $list['info']=(new Query())->select('nickName,avatarUrl,phone,wechat,time')->from('yii_user')->where($where)->one();
      $list['info']['time']=date('Y-m-d',$list['info']['time']);
      $list['education']=(new Query())->select('id,school,major,start_time,end_time,education')->from('yii_user_education')->where($where)->all();
      $list['experience']=(new Query())->select('id,company,content,work,begin_time,end_time')->from('yii_user_experience')->where($where)->all();
      $list['hope']=(new Query())->select('id,name,region,type,min_salary,max_salary')->from('yii_user_hope')->where($where)->all();
      return json_encode($list);
    }

    //更新个人信息
    public function actionUpdate(){
        $where['user_id'] = $this->user_id;
        $post = \Yii::$app->request->post();
        $post['update_time']=time();
        $post['time']=time();
        unset($post['token']);
        $res = \Yii::$app->db->createCommand()->update('yii_user',$post, $where)->execute();
        if ($res) {
            return json_encode(['code' => 1, 'msg' => '更新成功']);
        } else {
            return json_encode(['code' => 0, 'msg' => '更新失败']);
        }
    }


    //个人信息
    public function actionInfo(){
        $where['user_id'] = $this->user_id;
        $list = (new Query())->select('user_id,nickName,avatarUrl,gender,birthday,phone,wechat,work_time')->from('yii_user')->where($where)->one();
        return json_encode($list);
    }

    //查看个人评价
    public function actionEvaluate(){
        $method=\Yii::$app->request->method;
        $where['user_id'] = $this->user_id;
        if ($method=='POST'){
         $data['content']=\Yii::$app->request->post()['content'];
         $res=\Yii::$app->db->createCommand()->update('yii_user_evaluate',$data,$where)->execute();
               $time['time']=time();
              \Yii::$app->db->createCommand()->update('yii_user',$time, $where)->execute();
            if ($res) {
                return json_encode(['code' => 1, 'msg' => '更新成功']);
            } else {
                return json_encode(['code' => 0, 'msg' => '无内容更新']);
            }
        }else{
            $list = (new Query())->select('*')->from('yii_user_evaluate')->where($where)->one();
            if (!$list) {
                \Yii::$app->db->createCommand()->insert('yii_user_evaluate', $where)->execute();
                $list = (new Query())->select('*')->from('yii_user_evaluate')->where($where)->one();
            }
            return json_encode($list);
        }
    }

    //报名记录
    public function actionLog(){
        $where['user_id']=$this->user_id;
        $list=(new Query())->select('id,work_id,create_time')->from('yii_user_work')->where($where)->all();
        foreach ($list as $k=>$v){
            $list[$k]['work']=$this->GetWork($v['work_id']);
            $list[$k]['log_id']=$v['id'];
            $list[$k]['create_time']=date('Y.m.d',$v['create_time']);
            unset($list[$k]['work_id']);
            unset($list[$k]['id']);
        }
        return json_encode($list);

    }

    //岗位信息
    public function GetWork($work_id){
        $list=(new Query())->select('id,name,city,on,region,company,experience,education,min_salary,max_salary,welfare')->from('yii_work')->where(['id'=>$work_id])->one();
        $list['city']=$this->GetRegion($list['city']);
        $list['region']=$this->GetRegion($list['region']);
        $list['experience']=$this->GetExperience($list['experience']);
        $list['education']=$this->GetEducation($list['education']);
        $list['welfare']=$this->GetWelfare($list['welfare']);
        $list['company']=$this->GetCompany($list['company']);
        return $list;
    }


    //简历附件上传
    public function actionAnnex(){
        $transaction = \Yii::$app->db->beginTransaction();
        try {
        $where['user_id']=$this->GetUser()['user_id'];
       $time['time']=time();
        \Yii::$app->db->createCommand()->update('yii_user',$time, $where)->execute();
        
        $check=(new Query())->select('*')->from('yii_user_file')->where($where)->count();
        if ($check<2){
         \Yii::$app->db->createCommand()->insert('yii_user_file',$where)->execute();
         $id=\Yii::$app->db->getLastInsertID();
        }else{
            return json_encode(['code'=>0,'msg'=>'最多上传两份简历附件']);
        }

        $imageFile =UploadedFile::getInstanceByName('file');
        $type=$imageFile->type;

          
        if(!($type=='image/jpeg'||$type=='image/png'||$type=='application/octet-stream'||$type=='application/pdf'||$type=='application/msword'||$type=='application/vnd.openxmlformats-officedocument.wordprocessingml.document')){
            return json_encode(['code'=>0,'msg'=>'简历附件仅支持JPEG、PNG、DOC、PDF格式']);
        }

        $size=round($imageFile->size/1024/1024,2);
        if ($size>2){
            return json_encode(['code'=>0,'msg'=>'文件大小不能超过2M']);
        }

        $pathurl='file/'.date('Y-m-d');
        if (!file_exists($pathurl)) {
            mkdir($pathurl,0777,true);
        }
        $path=$pathurl.'/'.md5(uniqid(6).$imageFile->baseName).'.'.$imageFile->extension;
        $imageFile->saveAs($path);

        $data['name']='个人简历';
        $data['size']=$size;
        $data['url']=$path;
        $data['type']=$imageFile->extension;
        $data['create_time']=time();
        \Yii::$app->db->createCommand()->update('yii_user_file',$data,array('id'=>$id))->execute();
            $transaction->commit();
            return json_encode(['code'=>1,'msg'=>'上传成功']);
        } catch (\Exception $e) {
            $transaction->rollBack();
            return json_encode(['code'=>0,'msg'=>'上传失败']);
        }

    }

    //简历附件列表
    public function actionAnnexlist(){
        $where['user_id']=$this->GetUser()['user_id'];
        $list=(new Query())->select('id,name,size,url,type,create_time')->from('yii_user_file')->where($where)->all();
        foreach ($list as $k=>$v){
            $list[$k]['create_time']=date('Y-m-d',$v['create_time']);
            $list[$k]['url']='https://'.$_SERVER['HTTP_HOST'].'/'.$v['url'];
        }

        return json_encode($list);
    }

    //删除附件
    public function actionAnnexdel(){
    	 $where['user_id']=$this->GetUser()['user_id'];
         $where['id']=$this->PostId();
         $transaction = \Yii::$app->db->beginTransaction();
         try {
         $info=(new Query())->select('*')->from('yii_user_file')->where($where)->one();
         unlink($info['url']);
         \Yii::$app->db->createCommand()->delete('yii_user_file',$where)->execute();
         $transaction->commit();
         return json_encode(['code'=>1,'msg'=>'删除成功']);
        } catch (\Exception $e) {
         $transaction->rollBack();
         return json_encode(['code'=>0,'msg'=>'删除失败']);
        }
    }


    //头像上传
    public function actionUpload(){
        $transaction = \Yii::$app->db->beginTransaction();
        try {
        $imageFile =UploadedFile::getInstanceByName('file');
        
        $size=round($imageFile->size/1024/1024,2);
        if ($size>2){
            return json_encode(['code'=>0,'msg'=>'文件大小不能超过2M']);
        }

        $pathurl='file/'.date('Y-m-d');
        if (!file_exists($pathurl)) {
            mkdir($pathurl,0777,true);
        }
        $path=$pathurl.'/'.md5(uniqid(6).$imageFile->baseName).'.'.$imageFile->extension;
        $imageFile->saveAs($path);
        $transaction->commit();
         return json_encode(['code'=>1,'msg'=>'上传成功','url'=>'https://'.$_SERVER['HTTP_HOST'].'/'.$path]);
        } catch (\Exception $e) {
          $transaction->rollBack();
         return json_encode(['code'=>0,'msg'=>'上传失败']);
        }

    }


}